/*
 *  dna_timertask.h -- provide dna-system timer task framework. 
 *
 *  Now system support three types of timer task:
 *
 *  @ General timer (yyyy-mm-dd hh:mm:ss)
 *  @ Delay timer (yyyy-mm-dd hh:mm:ss)
 *  @ Period timer (hh:mm:ss ww), similar to the alarm clock, 
 *       Users can set at the same time every day to perform the specified task
 *
 *  Error code please refer to dna_errno.h
 *
 *  ORIGINAL AUTHOR: Xu Chun (chun.xu@broadlink.com.cn)
 *
 *  Copyright (c) 2016 Broadlink Corporation
 */

#ifndef __DNA_TIMERTASK_H
#define __DNA_TIMERTASK_H

#ifdef __cplusplus
    extern "C" {
#endif

#if DNA_TIMER_EXTEND_ENABLE
#define DNA_TIMER_TASK_MAX_CNT                       48
#define DNA_TIMER_CMD_LIMIT_SIZE					 64
#else
#define DNA_TIMER_TASK_MAX_CNT                       16
#endif
#define DNA_TIMER_CMD_BUFF_SIZE                      200

#define DNA_TIMERTASK_BITMAP_SIZE                    (DNA_TIMER_TASK_MAX_CNT / 8)
#define DNA_PSM_TIMER_TASK_MAX_CNT	                 2
#define DNA_TIMER_TASK_ALL                           0xFF
#define PSM_CMD_PART_LEN \
	(sizeof(dna_cycle_timer_t) - sizeof(dna_interval_timer_desc_t))

/* dna-system now support three types of timer task */
typedef enum {
    /* yyyy-mm-dd hh:mm:ss */
    DNA_GENERAL_TIMER = 0,
    /* yyyy-mm-dd hh:mm:ss */
    DNA_DELAY_TIMER,
    /* hh:mm:ss ww */
    DNA_PERIOD_TIMER,
    /* hh:mm:ss hh:mm:ss ww */
    DNA_CYCLE_TIMER,
    /* hh:mm:ss hh:mm:ss ww */
    DNA_RAND_TIMER,
    /* ... */
    DNA_TIMER_MAX,
} dna_timertask_type_e;

/* dna-system timer task operation code */
typedef enum {
    DNA_TIMER_ADD = 0,
    DNA_TIMER_DEL,
    DNA_TIMER_EDIT,
    DNA_TIMER_GET,
    DNA_TIMER_EXTEND_EDIT,
    DNA_TIMER_OPT_MAX,
} dna_timertask_opt_e;

/* dna-system timer task operation subcode */
typedef enum {
    DNA_TIMER_GET_DATA = 0,
    DNA_TIMER_GET_LIST,
    DNA_TIMER_GET_TIMER,
    DNA_TIMER_GET_DESC,
    DNA_TIMER_GET_BITMAP,
    DNA_TIMER_GET_STATE,
    DNA_TIMER_EDIT_DESC,        /* Only used in DNA_TIMER_EXTEND_EDIT */
    DNA_TIMER_EDIT_CMD,         /* Only used in DNA_TIMER_EXTEND_EDIT */
    DNA_TIMER_EDIT_STATE,
} dna_timertask_subopt_e;

/* dna-system timer task event code */
typedef enum {
    DNA_TIMER_TASK_CHANGE = 0,
    DNA_TIMER_TASK_PERFORM_DONE,
    DNA_TIMER_TASK_PERFORM_FAIL,
    DNA_TIMER_TASK_UNPERFORM,
} dna_timertask_event_e;

typedef enum {
	DNA_PSM_TIMER_CMD1 = 4,
	DNA_PSM_TIMER_CMD2 = 6,
	DNA_PSM_TIMER_DISABLE = 8,
	DNA_PSM_TIMER_DONE = 10,
} dna_cycletimer_event_e;

typedef enum {
    DNA_TIME_DESC_PACK = 0,
    DNA_TIME_DESC_UNPACK,
    DNA_TIME_DESC_MSG_MAX,
}dna_time_desc_msg_e;

typedef enum {
    DNA_TIME_SNAP_UPDATE_NONE = 0,
    DNA_TIME_SNAP_UPDATE_INFO,
    DNA_TIME_SNAP_UPDATE_MASK,
    DNA_TIME_SNAP_UPDATE_LIST,
    DNA_TIME_SNAP_UPDATE_MSG,
}dna_time_snap_msg_e;
typedef enum {
    DNA_SUN_SNAP_ERR = -1,
    DNA_SUN_SNAP_INIT,
    DNA_SUN_SNAP_NONE,
    DNA_SUN_SNAP_REQ,
    DNA_SUN_SNAP_INFO,
    DNA_SUN_SNAP_LIST,
    DNA_SUN_SNAP_MSG,
}dna_sun_state_e;
/*
*  dna-system timertask index bitmap.
*  If one bit was set, then means that the position exist timertask.
*/
typedef struct dna_timertask_bitmap {
    int count;
    unsigned char mask[DNA_TIMERTASK_BITMAP_SIZE];
} dna_timertask_bitmap_t;

/* The following structure need byte alignment */
#pragma pack(push)
#pragma pack(1)

/* dna-system general timer task descriptor */
typedef struct dna_general_timer_desc {
    unsigned char index;
    unsigned char enable;
    unsigned short year;
    unsigned char minute;
    unsigned char second;
    unsigned char hour;
    unsigned char day;
    unsigned char month;
#if defined(CONFIG_GREEHOME_APP)
	unsigned char name[32];
#endif
    /* Cloud_id: cloud platform unique ID, refer to dna_cloud.h
      (dna_cloud_id_e) */
    unsigned char reserved[7];
	unsigned char cloud_id;
} dna_general_timer_desc_t;

/* dna-system period timer task descriptor */
typedef struct dna_period_timer_desc {
    unsigned char index;
    unsigned char enable;
    /* Bit0 - Sunday, Bit1 - Monday , ... , Bit6 - Saturday */
    unsigned char week;
    unsigned char hour;
    unsigned char minute;
    unsigned char second;
#if defined(CONFIG_GREEHOME_APP)
	unsigned char name[32];
#endif
    /* Cloud_id: cloud platform unique ID, refer to dna_cloud.h
      (dna_cloud_id_e) */
    unsigned char reserved[7];
    unsigned char cloud_id;
} dna_period_timer_desc_t;

/* dna-system interval timer task descriptor */
typedef struct dna_interval_timer_desc {
    unsigned char index;
    unsigned char enable;
    unsigned char start_hour;
    unsigned char start_minute;
    unsigned char start_second;
    unsigned char end_hour;
	unsigned char end_minute;
	unsigned char end_second;
	unsigned int time1_count;
	unsigned int time2_count;
	unsigned char week;
#if defined(CONFIG_GREEHOME_APP)
	unsigned char name[32];
#endif
    /* Cloud_id: cloud platform unique ID, refer to dna_cloud.h
      (dna_cloud_id_e) */
    unsigned char reserved[7];
    unsigned char cloud_id;
} dna_interval_timer_desc_t;

/* dna-system general timer task */
typedef struct dna_general_timer {
    dna_general_timer_desc_t info;
    unsigned char len;
    unsigned char cmd[DNA_TIMER_CMD_BUFF_SIZE];
} dna_general_timer_t;

/* dna-system delay timer task */
typedef struct dna_delay_timer {
    dna_general_timer_desc_t info;
    unsigned char len;
    unsigned char cmd[DNA_TIMER_CMD_BUFF_SIZE];
} dna_delay_timer_t;

/* dna-system period timer task */
typedef struct dna_period_timer {
    dna_period_timer_desc_t info;
    unsigned char len;
    unsigned char cmd[DNA_TIMER_CMD_BUFF_SIZE];
} dna_period_timer_t;

typedef struct dna_cycle_timer {
	dna_interval_timer_desc_t info;
	unsigned char len1;
	unsigned char cmd1[DNA_TIMER_CMD_BUFF_SIZE];
	unsigned char len2;
	unsigned char cmd2[DNA_TIMER_CMD_BUFF_SIZE];
} dna_cycle_timer_t;

typedef struct dna_rand_timer {
	dna_interval_timer_desc_t info;
	unsigned char len1;
	unsigned char cmd1[DNA_TIMER_CMD_BUFF_SIZE];
	unsigned char len2;
	unsigned char cmd2[DNA_TIMER_CMD_BUFF_SIZE];
} dna_rand_timer_t;

/* dna-system general timer task list */
typedef struct dna_general_timer_list {
    unsigned short count;
    dna_general_timer_desc_t timer[DNA_TIMER_TASK_MAX_CNT];
} dna_general_timer_list_t;

/* dna-system delay timer task list */
typedef struct dna_delay_timer_list {
    unsigned short count;
    dna_general_timer_desc_t timer[DNA_TIMER_TASK_MAX_CNT];
} dna_delay_timer_list_t;

/* dna-system period timer task list */
typedef struct dna_period_timer_list {
    unsigned short count;
    dna_period_timer_desc_t timer[DNA_TIMER_TASK_MAX_CNT];
} dna_period_timer_list_t;

/* dna-system cycle timer task list */
typedef struct dna_cycle_timer_list {
	unsigned short count;
	dna_interval_timer_desc_t timer[DNA_PSM_TIMER_TASK_MAX_CNT];
} dna_cycle_timer_list_t;

/* dna-system rand timer task list */
typedef struct dna_rand_timer_list {
	unsigned short count;
	dna_interval_timer_desc_t timer[DNA_PSM_TIMER_TASK_MAX_CNT];
} dna_rand_timer_list_t;

/* dna-system timertask perform notify descriptor */
typedef struct dna_timertask_perform_notify {
	unsigned char type;
	union {
		dna_general_timer_desc_t general;
		dna_period_timer_desc_t period;
	};
} dna_timertask_perform_notify_t;

/* Restore previous alignment */
#pragma pack(pop)

/* dna-system timer task perform callback interface */
typedef int (* dna_timertask_handle_t)(
                   /* Cloud_id: cloud platform unique ID, refer to dna_cloud.h
                              (dna_cloud_id_e) */
                   unsigned int cloud_id,
				   /* refer to dna_timertask_type_e */
				   unsigned int type,
				   /* task index */
				   unsigned int index,
                   /* Input: command data */
                   const unsigned char * input, unsigned int len);

/* dna-system timer task event notify callback interface */
typedef int (* dna_timertask_event_notify_t)(
                   /* Event: event code, refer to dna_timertask_event_e */
                   unsigned int event,
                   /* Input: event data */
                   const void * input, unsigned int len,
                   /* Arg: user parameters */
                   void * arg);

/*
*  dna-system timer task framework: timer task polling interface.
*  Check timer task whether expires.
*
*  Return TRUE means found expired timer task.
*/
int dna_timertask_poll(int timezone);

/*
*  dna-system timer task framework: timer task perform interface.
*
*  Notice: user call it when dna_timertask_poll API return TRUE,
*  Consider of perform maybe blocked, so don't call it in interrupt context.
*/
void dna_timertask_perform(void);

/*
*  dna-system timer task framework: timer task operation (Add/Del/Edit/Get) interface.
*
*  @opcode: timer task operation code, refer to dna_timertask_opt_e
*  @cloud_id: cloud platform unique ID, refer to dna_cloud.h (dna_cloud_id_e) 
*  @input: input data
*  @len: input data valid length
*  @output: output buffer
*  @maxsize: output buffer size
*
*  Return timer task operation result.
*/
int dna_timertask_operate(
        unsigned int opcode, unsigned int cloud_id,
        const unsigned char * input, unsigned int len,
        unsigned char * output, unsigned int maxsize);

/*
*  dna-system timer task framework: timer task event register interface.
*
*  @notify: event notify interface
*  @arg: user parameters, will push to notify interface
*
*  Notice: notify interface unable to call dna_timertask_operate.
*
*  Return 0 means event register success.
*/
int dna_timertask_event_register(dna_timertask_event_notify_t notify, void * arg);

/* 
*  dna-system timer task framework initialization.
*
*  @handle: timer task perform handle
*
*  Return 0 means initialize success, if return -1 means occurred error.
*/
int dna_timertask_init(dna_timertask_handle_t handle);

/* 
*  dna-system timer task snap refresh .
*
*  @handle: index is  dna_timertask_type_e; 
*
*  Return 0 means initialize success, if return -1 means occurred error.
*/
int dna_timer_snap_refresh(int index);


#ifdef __cplusplus
}
#endif

#endif
